package cn.zust.itcost.controller;

import cn.zust.itcost.entity.graph.node.ResumeNode;
import cn.zust.itcost.service.ResumeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/*简历管理功能*/

@RestController
@RequestMapping("/api/resume")
@CrossOrigin(origins = "http://localhost:8090")
@Api(tags = "简历管理接口")
public class ResumeController {

    @Autowired
    private ResumeService resumeService;

    @PostMapping("/upload")
    @ApiOperation("上传并解析简历")
    public ResponseEntity<Map<String, Object>> uploadResume(@RequestParam("file") MultipartFile file) {
        try {
            Map<String, Object> result = resumeService.parseAndSaveResume(file);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(Map.of("error", e.getMessage()));
        }
    }

    @PostMapping("/submit")
    @ApiOperation("提交简历表单数据")
    public ResponseEntity<Map<String, Object>> submitResume(@RequestBody Map<String, String> formData) {
        try {
            Map<String, Object> result = resumeService.saveResumeForm(formData);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(Map.of("error", e.getMessage()));
        }
    }

    @GetMapping("/{id}")
    @ApiOperation("获取简历详情")
    public ResponseEntity<ResumeNode> getResume(@PathVariable Long id) {
        ResumeNode resume = resumeService.getResumeById(id);
        if (resume != null) {
            return ResponseEntity.ok(resume);
        }
        return ResponseEntity.notFound().build();
    }

    @GetMapping("/search")
    @ApiOperation("搜索简历")
    public ResponseEntity<Iterable<ResumeNode>> searchResumes(@RequestParam String keyword) {
        return ResponseEntity.ok(resumeService.searchResumes(keyword));
    }

    @GetMapping("/all")
    @ApiOperation("获取所有简历")
    public ResponseEntity<Iterable<ResumeNode>> getAllResumes() {
        return ResponseEntity.ok(resumeService.getAllResumes());
    }
}
